const logger = require('../helper/logger')

const tableName = 'administrator'

module.exports = async (db) => {
  try {
    const exists = await db.schema.hasTable(tableName)
    if (!exists) {
      await db.schema.createTable(tableName, (table) => {
        table.charset('utf8')
        table.increments('id')
        table.string('full_name').notNullable()
        table.string('username')
        table.string('email').unique()
        table.string('password')
        table.string('phone_region')
        table.string('phone_number')
        table.unique(['phone_region', 'phone_number'])
        table.string('profile_image_url')
        table.boolean('enabled').defaultTo(false).notNullable()
        table.timestamps(false, true)
      })
      logger.info(`Created ${tableName} table`)
      const items = [
        {
          full_name: 'Paifile Admin',
          email: 'admin@paifile.com',
          password: '$2a$13$d2loY4ckCVLs42NBviCSyOQBUVuVi.Fs16JU2itDg3tosQD8gGmZy',
          enabled: true,
        },
        {
          full_name: 'KK Chen',
          email: 'kk@bichenkk.com',
          password: '$2a$13$d2loY4ckCVLs42NBviCSyOQBUVuVi.Fs16JU2itDg3tosQD8gGmZy',
          phone_region: 852,
          phone_number: 97189571,
          profile_image_url: 'https://s3-ap-southeast-1.amazonaws.com/binatir.com/assets/customised/avatar-kk.jpg',
          enabled: true,
        },
        {
          full_name: 'Max Luk',
          email: 'max@binatir.com',
          password: '$2a$13$d2loY4ckCVLs42NBviCSyOQBUVuVi.Fs16JU2itDg3tosQD8gGmZy',
          phone_region: 852,
          phone_number: 64401985,
          profile_image_url: 'https://binatir.com/assets/customised/avatar-max.jpeg',
          enabled: true,
        },
      ]
      await db.batchInsert(tableName, items).then()
    }
  } catch (error) {
    logger.error(`Error creating ${tableName} table`, error)
  }
}
